package cn.ibizlab.odoo.core.odoo_snailmail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Isnailmail_letter;
import cn.ibizlab.odoo.core.odoo_snailmail.domain.Snailmail_letter;
import cn.ibizlab.odoo.core.odoo_snailmail.filter.Snailmail_letterSearchContext;
import cn.ibizlab.odoo.core.odoo_snailmail.service.ISnailmail_letterService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[Snailmail 信纸] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Snailmail_letterServiceImpl implements ISnailmail_letterService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Isnailmail_letterClientService snailmail_letterClientService;

    @Override
    public void createBatch(List<Snailmail_letter> snailmail_letters, int batchSize) {

                        //snailmail_letterClientService.createBatch(snailmail_letters,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Isnailmail_letter clientModel = snailmail_letterClientService.createModel();
        clientModel.setId(id);
        snailmail_letterClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //snailmail_letterClientService.removeBatch(snailmail_letters) ;

    }

    @Override
    public Snailmail_letter get(Integer id) {
        Isnailmail_letter clientModel = snailmail_letterClientService.createModel();
        clientModel.setId(id);
        snailmail_letterClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Snailmail_letter get(Snailmail_letter snailmail_letter) {
        Snailmail_letter retsnailmail_letter = this.get(snailmail_letter.getId()) ;
    	BeanCopier copier=BeanCopier.create(retsnailmail_letter.getClass(), snailmail_letter.getClass(), false);
        copier.copy(retsnailmail_letter, snailmail_letter, null);
        return snailmail_letter ;
    }

    @Override
    public void updateBatch(List<Snailmail_letter> snailmail_letters, int batchSize) {

        //snailmail_letterClientService.updateBatch(snailmail_letters,batchSize) ;

    }

    @Override
    public boolean update(Snailmail_letter snailmail_letter) {
        boolean bOk = false ;
        Isnailmail_letter clientModel = this.convert2Model(snailmail_letter,null);
        snailmail_letterClientService.update(clientModel) ;
        this.convert2Domain(clientModel,snailmail_letter) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean create(Snailmail_letter snailmail_letter) {
        boolean bOk = false ;
        Isnailmail_letter clientModel = this.convert2Model(snailmail_letter,null);
        snailmail_letterClientService.create(clientModel) ;
        this.convert2Domain(clientModel,snailmail_letter);
        bOk = true ;

        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Snailmail_letter> searchDefault(Snailmail_letterSearchContext context) {
        List<Snailmail_letter> list = new ArrayList<Snailmail_letter>();
        Page<cn.ibizlab.odoo.core.client.model.Isnailmail_letter> clientModelList = snailmail_letterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isnailmail_letter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Snailmail_letter>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据公司查询
     */
    @Override
    public Page<Snailmail_letter> selectByOdoo_company(Integer id){
        Snailmail_letterSearchContext context = new Snailmail_letterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Snailmail_letter> list = new ArrayList<Snailmail_letter>();
        Page<cn.ibizlab.odoo.core.client.model.Isnailmail_letter> clientModelList = snailmail_letterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isnailmail_letter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Snailmail_letter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Snailmail_letter> selectByOdoo_partner(Integer id){
        Snailmail_letterSearchContext context = new Snailmail_letterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Snailmail_letter> list = new ArrayList<Snailmail_letter>();
        Page<cn.ibizlab.odoo.core.client.model.Isnailmail_letter> clientModelList = snailmail_letterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isnailmail_letter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Snailmail_letter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Snailmail_letter> selectByOdoo_create(Integer id){
        Snailmail_letterSearchContext context = new Snailmail_letterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Snailmail_letter> list = new ArrayList<Snailmail_letter>();
        Page<cn.ibizlab.odoo.core.client.model.Isnailmail_letter> clientModelList = snailmail_letterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isnailmail_letter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Snailmail_letter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Snailmail_letter> selectByOdoo_user(Integer id){
        Snailmail_letterSearchContext context = new Snailmail_letterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Snailmail_letter> list = new ArrayList<Snailmail_letter>();
        Page<cn.ibizlab.odoo.core.client.model.Isnailmail_letter> clientModelList = snailmail_letterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isnailmail_letter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Snailmail_letter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Snailmail_letter> selectByOdoo_write(Integer id){
        Snailmail_letterSearchContext context = new Snailmail_letterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Snailmail_letter> list = new ArrayList<Snailmail_letter>();
        Page<cn.ibizlab.odoo.core.client.model.Isnailmail_letter> clientModelList = snailmail_letterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isnailmail_letter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Snailmail_letter>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Isnailmail_letter convert2Model(Snailmail_letter domain , Isnailmail_letter model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = snailmail_letterClientService.createModel() ;
        }

         if(domain.getStateDirtyFlag())
            model.setState(domain.getState());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getDuplexDirtyFlag())
            model.setDuplex(domain.getDuplex());
         if(domain.getColorDirtyFlag())
            model.setColor(domain.getColor());
         if(domain.getModelDirtyFlag())
            model.setModel(domain.getModel());
         if(domain.getAttachment_idDirtyFlag())
            model.setAttachment_id(domain.getAttachment_id());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getReport_templateDirtyFlag())
            model.setReport_template(domain.getReport_template());
         if(domain.getInfo_msgDirtyFlag())
            model.setInfo_msg(domain.getInfo_msg());
         if(domain.getRes_idDirtyFlag())
            model.setRes_id(domain.getRes_id());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getPartner_id_textDirtyFlag())
            model.setPartner_id_text(domain.getPartner_id_text());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getUser_id_textDirtyFlag())
            model.setUser_id_text(domain.getUser_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getUser_idDirtyFlag())
            model.setUser_id(domain.getUser_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getPartner_idDirtyFlag())
            model.setPartner_id(domain.getPartner_id());
        return model ;
    }

    public Snailmail_letter convert2Domain( Isnailmail_letter model ,Snailmail_letter domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Snailmail_letter();
        }

        if(model.getStateDirtyFlag())
            domain.setState(model.getState());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getDuplexDirtyFlag())
            domain.setDuplex(model.getDuplex());
        if(model.getColorDirtyFlag())
            domain.setColor(model.getColor());
        if(model.getModelDirtyFlag())
            domain.setModel(model.getModel());
        if(model.getAttachment_idDirtyFlag())
            domain.setAttachment_id(model.getAttachment_id());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getReport_templateDirtyFlag())
            domain.setReport_template(model.getReport_template());
        if(model.getInfo_msgDirtyFlag())
            domain.setInfo_msg(model.getInfo_msg());
        if(model.getRes_idDirtyFlag())
            domain.setRes_id(model.getRes_id());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getPartner_id_textDirtyFlag())
            domain.setPartner_id_text(model.getPartner_id_text());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getUser_id_textDirtyFlag())
            domain.setUser_id_text(model.getUser_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getUser_idDirtyFlag())
            domain.setUser_id(model.getUser_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getPartner_idDirtyFlag())
            domain.setPartner_id(model.getPartner_id());
        return domain ;
    }


}
